package kr.co.mustgo.model.dao.review;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import kr.co.mustgo.domain.review.Comment;
import kr.co.mustgo.domain.review.ReviewsBoard;
import kr.co.mustgo.util.BoardUtilities;

@Repository
public class ReviewsDAOImpl implements ReviewsDAO{

	@Autowired
	private SqlSessionTemplate session;
	
	//새 게시물 하나를 insert
	@Override
	public int insertPostForNewCount(ReviewsBoard board) {
		return session.insert(namespace("insertPostForNewCount"), board);
	}

	//매개변수로 넘어온 페이지의 목록 조회
	@Override
	public List<ReviewsBoard> selectReviewsBoardList(int page) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		return session.selectList(namespace("selectReviewsBoardList"), map);
	}

	//총 게시물의 수 return 
	@Override
	public int selectTotalPost() {
		return session.selectOne(namespace("selectTotalPost"));
	}

	//조회수 1증가
	@Override
	public int updateViewCount(int no) {
		return session.update(namespace("updateViewCount"), no);
	}

	//글번호로 게시물 하나를 조회
	@Override
	public ReviewsBoard selectPostByNo(int no) {
		return session.selectOne(namespace("selectPostByNo"), no);
	}

	//게시물 하나를 수정
	@Override
	public int updatePost(ReviewsBoard board) {
		return session.update(namespace("updatePost"), board);
	}

	//글번호로 게시물을 삭제
	@Override
	public int deletePostByNo(int no) {
		return session.delete(namespace("deletePostByNo"), no);
	}

	@Override
	public int insertComment(Comment comment) {
		return session.insert(namespace("insertComment"), comment);
	}

	@Override
	public int updateComment(Comment comment) {
		return session.update(namespace("updateComment"), comment);
	}

	@Override
	public int deleteComment(int cmNo) {
		return session.delete(namespace("deleteComment"), cmNo);
	}

	@Override
	public Comment selectCommentBycmNo(int cmNo) {
		return session.selectOne(namespace("selectCommentBycmNo"), cmNo);
	}

	@Override
	public List<Comment> selectCommentByNo(int no) {
		return session.selectList(namespace("selectCommentByNo"), no);
	}
	
	private String namespace(String id){
		return "review.travel."+id;
	}

	@Override
	public int deleteCommentByNo(int no) {
		return session.delete(namespace("deleteCommentByNo"), no);
	}

	//검색된 게시물의 총수
	@Override
	public int selectTotalSearch(Map map) {
		return session.selectOne(namespace("selectTotalSearch"), map);
	}

	//검색된 게시물들의 페이지
	@Override
	public List<ReviewsBoard> selectSearchList(Map map) {
		return session.selectList(namespace("selectSearchList"), map);
	}
}




